package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.cache.BloomName;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.service.SkuInfoService;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author dyqstart
 * @create 2021-08-10-20:44
 * @since 1.0.0
 */
@RestController
public class BloomFilterAddDataController {

    @Qualifier(BloomName.SKU)//每一个bean的方法就是方法名
    @Autowired
    RBloomFilter<Object> skufilter;

    @Autowired
    SkuInfoService skuInfoService;

    /**
     * 布隆重建
     * @return
     */
    @GetMapping("/add/bloom/sku")
    public Result bloomAddAll() {

        skufilter.delete();//删除旧布隆数据

        skufilter.tryInit(1000000L,0.001);

        List<Long> skuIds = skuInfoService.getAllId();

        for (Long skuId : skuIds) {

            //重建布隆
            String cacheKey = RedisConst.SKUKEY_PREFIX + skuId + RedisConst.SKUKEY_SUFFIX;
            skufilter.add(cacheKey);

        }

        return Result.ok();
    }
}
